/*
 *  (C) Copyright 2008 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.dao.query.hibernate;

import java.util.HashMap;
import java.util.Map;

import org.openinsula.vulcano.orm.dao.query.spec.AbstractGroupSpec;
import org.openinsula.vulcano.orm.dao.query.spec.AbstractSingleValuePropertySpec;
import org.openinsula.vulcano.orm.dao.query.spec.AndSpec;
import org.openinsula.vulcano.orm.dao.query.spec.BetweenSpec;
import org.openinsula.vulcano.orm.dao.query.spec.EqualsSpec;
import org.openinsula.vulcano.orm.dao.query.spec.GreaterOrEqualSpec;
import org.openinsula.vulcano.orm.dao.query.spec.GreaterSpec;
import org.openinsula.vulcano.orm.dao.query.spec.InSpec;
import org.openinsula.vulcano.orm.dao.query.spec.LesserOrEqualSpec;
import org.openinsula.vulcano.orm.dao.query.spec.LesserSpec;
import org.openinsula.vulcano.orm.dao.query.spec.LikeSpec;
import org.openinsula.vulcano.orm.dao.query.spec.NotEqualsSpec;
import org.openinsula.vulcano.orm.dao.query.spec.NotNullSpec;
import org.openinsula.vulcano.orm.dao.query.spec.NullSpec;
import org.openinsula.vulcano.orm.dao.query.spec.OrSpec;
import org.openinsula.vulcano.orm.dao.query.spec.Spec;
import org.openinsula.vulcano.orm.dao.query.spec.SpecVisitor;

/**
 * 
 * @author erdanielli
 * 
 */
@SuppressWarnings("unchecked")
public class HqlSpecVisitorImpl implements SpecVisitor<String> {
	private Map<Class<? extends AbstractGroupSpec>, String> groupSpecMapping;

	private Map<Class<? extends AbstractSingleValuePropertySpec>, String> singleValuePropertySpecMapping;

	public HqlSpecVisitorImpl() {
		initializeMappings();
	}

	private void initializeMappings() {
		groupSpecMapping = new HashMap<Class<? extends AbstractGroupSpec>, String>();
		groupSpecMapping.put(AndSpec.class, " and ");
		groupSpecMapping.put(OrSpec.class, " or ");

		singleValuePropertySpecMapping = new HashMap<Class<? extends AbstractSingleValuePropertySpec>, String>();
		singleValuePropertySpecMapping.put(EqualsSpec.class, "=");
		singleValuePropertySpecMapping.put(NotEqualsSpec.class, "<>");
		singleValuePropertySpecMapping.put(GreaterSpec.class, ">");
		singleValuePropertySpecMapping.put(GreaterOrEqualSpec.class, ">=");
		singleValuePropertySpecMapping.put(LesserSpec.class, "<");
		singleValuePropertySpecMapping.put(LesserOrEqualSpec.class, "<=");
	}

	private String groupSpecVisit(final AbstractGroupSpec spec) {
		final String operation = groupSpecMapping.get(spec.getClass());

		StringBuilder sb = new StringBuilder();
		sb.append("(");

		Spec[] expressions = spec.getExpressions();
		sb.append(expressions[0].accept(this));

		for (int i = 1; i < expressions.length; i++) {
			sb.append(operation);
			sb.append(expressions[i].accept(this));
		}

		sb.append(")");
		return sb.toString();
	}

	private String singleValuePropertyVisit(final AbstractSingleValuePropertySpec<?> spec) {
		return String.format("%s.%s %s ?", HibernateVisitorConstants.DEFAULT_ALIAS, spec.getPropertyName(),
				singleValuePropertySpecMapping.get(spec.getClass()));
	}

	public String visit(final AndSpec andSpec) {
		return groupSpecVisit(andSpec);
	}

	public String visit(final OrSpec orSpec) {
		return groupSpecVisit(orSpec);
	}

	public String visit(final EqualsSpec<?> equalsSpec) {
		return equalsSpec.getValue() == null ? visit(new NullSpec(equalsSpec.getPropertyName()))
				: singleValuePropertyVisit(equalsSpec);
	}

	public String visit(final NotEqualsSpec<?> notEqualsSpec) {
		return notEqualsSpec.getValue() == null ? visit(new NotNullSpec(notEqualsSpec.getPropertyName()))
				: singleValuePropertyVisit(notEqualsSpec);
	}

	public String visit(final GreaterOrEqualSpec<?> greaterOrEqualSpec) {
		return singleValuePropertyVisit(greaterOrEqualSpec);
	}

	public String visit(final GreaterSpec<?> greaterSpec) {
		return singleValuePropertyVisit(greaterSpec);
	}

	public String visit(final LesserOrEqualSpec<?> lesserOrEqualSpec) {
		return singleValuePropertyVisit(lesserOrEqualSpec);
	}

	public String visit(final LesserSpec<?> lesserSpec) {
		return singleValuePropertyVisit(lesserSpec);
	}

	public String visit(final LikeSpec likeSpec) {
		String property = String.format("cast(%s.%s, string)", HibernateVisitorConstants.DEFAULT_ALIAS, likeSpec
				.getPropertyName());

		if (!likeSpec.isCaseSensitive()) {
			property = String.format("lower(%s)", property);
		}

		return property + " like ?";
	}

	public String visit(final NotNullSpec notNullSpec) {
		return String.format("%s.%s is not null", HibernateVisitorConstants.DEFAULT_ALIAS, notNullSpec
				.getPropertyName());
	}

	public String visit(final NullSpec nullSpec) {
		return String.format("%s.%s is null", HibernateVisitorConstants.DEFAULT_ALIAS, nullSpec.getPropertyName());
	}

	public String visit(final BetweenSpec betweenSpec) {
		return String.format("%s.%s between ? and ?", HibernateVisitorConstants.DEFAULT_ALIAS, betweenSpec
				.getPropertyName());
	}

	public String visit(final InSpec<?> inSpec) {
		StringBuilder sb = new StringBuilder();

		sb.append(HibernateVisitorConstants.DEFAULT_ALIAS);
		sb.append('.');
		sb.append(inSpec.getPropertyName());
		sb.append(" in (?");

		for (int i = 1, j = inSpec.getValues().length; i < j; i++) {
			sb.append(",?");
		}
		sb.append(")");

		return sb.toString();
	}

}
